unit fmcfg;

interface

uses
  MMSystem,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Mask, DLCtrlBase, DLXStdCtrls,
  DLXExtCtrls, DLGroupBar, ComCtrls, DLXButtons, DLEdit, DLComboBox;

type
  TCfgForm = class(TForm)
    dlgSound: TOpenDialog;
    DLGradientColorDrawer1: TDLGradientColorDrawer;
    RzPanel3: TDLXPanel;
    RzPanel2: TDLXPanel;
    imgtitle: TImage;
    lbtitle: TDLXLabel;
    DLGroupBar1: TDLGroupBar;
    DLGroup1: TDLGroup;
    RzPanel1: TDLXPanel;
    RzButton2: TDLXBitBtn;
    RzButton3: TDLXBitBtn;
    RzButton4: TDLXBitBtn;
    RzPageControl1: TPageControl;
    tsSound: TTabSheet;
    Label1: TLabel;
    DLXPanel1: TDLXPanel;
    Image1: TImage;
    Label2: TLabel;
    GBoxSound: TDLXPanel;
    Image2: TImage;
    radioDefaultSound: TRadioButton;
    radioCustomSound: TRadioButton;
    coboxSound: TDLComboBox;
    edtCustomSound: TDLEdit;
    btnSoundPlay: TDLXBitBtn;
    DLXSpeedButton1: TDLXSpeedButton;
    radioEnableSound: TRadioButton;
    radioDisableSound: TRadioButton;
    RzLabel1: TDLXLabel;
    procedure FormShow(Sender: TObject);
    procedure coboxSoundChange(Sender: TObject);
    procedure radioDisableSoundClick(Sender: TObject);
    procedure radioEnableSoundClick(Sender: TObject);
    procedure RzButton4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure radioDefaultSoundClick(Sender: TObject);
    procedure radioCustomSoundClick(Sender: TObject);
    procedure edtCustomSoundChange(Sender: TObject);
    procedure edtCustomSoundButtonClick(Sender: TObject);
    procedure btnSoundPlayClick(Sender: TObject);
    procedure RzButton2Click(Sender: TObject);
    procedure RzButton3Click(Sender: TObject);
  private
    { Private declarations }
    FSoundList: TStringList;
    procedure EnabledControls(ParentControl: TWinControl; bEnable: Boolean);
  public
    { Public declarations }
    procedure SaveCfg;
  end;

var
  CfgForm: TCfgForm;
procedure DoConfig;

implementation

uses uMsg;

{$r *.dfm}
procedure DoConfig;
begin
  if not Assigned(CfgForm) then
    CfgForm := TCfgForm.Create(Application);
  CfgForm.ShowModal;
  //CfgForm.BringToFront;
end;

procedure TCfgForm.coboxSoundChange(Sender: TObject);
//var
//  index: string;
begin
  if IMCFG.SoundList.Values[coboxSound.Text]='' then
    radioDefaultSound.Checked := True
  else begin
    radioCustomSound.Checked := True;
    edtCustomSound.Text := IMCFG.SoundList.Values[coboxSound.Text];
  end;
end;

procedure TCfgForm.edtCustomSoundButtonClick(Sender: TObject);
begin
  if dlgSound.Execute then
    edtCustomSound.Text := dlgSound.FileName;    //chenyan
end;

procedure TCfgForm.edtCustomSoundChange(Sender: TObject);
begin
  if radioCustomSound.Checked then
    FSoundList.Values[coboxSound.Text] := edtCustomSound.Text;
end;

procedure TCfgForm.EnabledControls(ParentControl: TWinControl; bEnable: Boolean);
var
  I: Integer;
begin
  ParentControl.Enabled := bEnable;
  for I := 0 to ParentControl.ControlCount - 1 do
  begin
//    Parent.Controls[i].Enabled := bEnable;
    if ParentControl.Controls[i] is TWinControl then
      EnabledControls(TWinControl(ParentControl.Controls[i]), bEnable)
    else
      ParentControl.Controls[i].Enabled := bEnable;
  end;
end;

procedure TCfgForm.FormCreate(Sender: TObject);
begin
  FSoundList := TStringList.Create;
end;

procedure TCfgForm.FormDestroy(Sender: TObject);
begin
  FSoundList.Free;
end;

procedure TCfgForm.FormShow(Sender: TObject);
begin
  radioEnableSound.Checked := IMCFG.EnableSound;
  radioDisableSound.Checked := not IMCFG.EnableSound;
  EnabledControls(GBoxSound,IMCFG.EnableSound);
  FSoundList.Assign(IMCFG.SoundList);
  if IMCFG.EnableSound then
    coboxSoundChange(CoboxSound);
end;

procedure TCfgForm.radioCustomSoundClick(Sender: TObject);
begin
  if radioCustomSound.Checked then
    FSoundList.Values[coboxSound.Text] := edtCustomSound.Text;
end;

procedure TCfgForm.radioDefaultSoundClick(Sender: TObject);
begin
  if radioDefaultSound.Checked then
    FSoundList.Values[coboxSound.Text] := '';
end;

procedure TCfgForm.radioDisableSoundClick(Sender: TObject);
begin
//  IMCFG.EnableSound := radioEnableSound.Checked;
  EnabledControls(GBoxSound, radioEnableSound.Checked);
end;

procedure TCfgForm.radioEnableSoundClick(Sender: TObject);
begin
  EnabledControls(GBoxSound, radioEnableSound.Checked);
end;

procedure TCfgForm.btnSoundPlayClick(Sender: TObject);
var
  sWaveRes: string;
begin
  if FSoundList.Values[coboxSound.Text]='' then
  begin
    case coboxSound.ItemIndex of
      0: sWaveRes := 'WAVE_MSG';
      1: sWaveRes := 'WAVE_SYS';
    end;
    PlaySound(PChar(sWaveRes), HInstance, SND_ASYNC or SND_RESOURCE)
  end
  else
    PlaySound(PChar(FSoundList.Values[coboxSound.Text]), HInstance, SND_ASYNC or SND_FILENAME)
end;

procedure TCfgForm.RzButton2Click(Sender: TObject);
begin
  SaveCfg;
  close;
end;

procedure TCfgForm.RzButton3Click(Sender: TObject);
begin
  Close;
end;

procedure TCfgForm.RzButton4Click(Sender: TObject);
begin
  SaveCfg;
end;

procedure TCfgForm.SaveCfg;
begin
  IMCFG.EnableSound := radioEnableSound.Checked;
  IMCFG.SoundList.Assign(FSoundList);
  IMCFG.SaveCfg;
end;

end.
